package com.ailanyin.soft.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.RuntimeUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 安装 Mysql
 *
 * @author ailanyin
 * @version 1.0
 * @since 2022-1-7 11:12:01
 */
public class MysqlUtil {

    private static void closeMysqlPassword() {
        List<String> list = new ArrayList<>();
        // 下面两句用来开启 binlog
        list.add("log-bin=/var/lib/mysql/mysql-bin");
        list.add("server-id=123454");
        list.add("skip-grant-tables");
        FileUtil.writeLines(list, "/etc/my.cnf", "UTF-8", true);
        threadSleep(10);
    }

    private static void openMysqlPassword() {
        List<String> list = FileUtil.readLines("/etc/my.cnf", "UTF-8");
        list.remove("skip-grant-tables");
        FileUtil.writeLines(list, "/etc/my.cnf", "UTF-8", false);
        threadSleep(11);
    }

    private static void threadSleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static void restartMysql() {
        System.out.println("========== 开始重启 Mysql ==========");
        RuntimeUtil.getResult(RuntimeUtil.exec("systemctl restart mysqld"));
        System.out.println("========== 重启完成 Mysql ==========");
    }

    private static String updateMysqlPassword() {
        // 密码: 大写 + 小写 + 符号
        String random = RandomUtil.randomStringUpper(1) + RandomUtil.randomString(13) + "A.";
        String sql = "update mysql.user set authentication_string=password('" + random + "') where user='root' and Host = 'localhost';";
        DbUtil.runSql(sql, "root");
        DbUtil.runSql("flush privileges;", "root");
        return random;
    }

    private static String updateMysqlPassword(String password) {
        // 密码: 大写 + 小写 + 符号
        String random = RandomUtil.randomStringUpper(1) + RandomUtil.randomString(13) + "A.";
        String sql = "mysql -uroot -p'" + password + "' -e \"alter user 'root'@'localhost' identified by '" + random + "';\" --connect-expired-password";
        List<String> list = new ArrayList<>();
        list.add(sql);
        FileUtil.writeLines(list, "/opt/updateMysqlPassword.txt", "UTF-8", false);
        threadSleep(11);
        RuntimeUtil.getResult(RuntimeUtil.exec("chmod -x /opt/updateMysqlPassword.txt"));
        RuntimeUtil.getResult(RuntimeUtil.exec("sh -x /opt/updateMysqlPassword.txt"));
        RuntimeUtil.getResult(RuntimeUtil.exec("rm -rf /opt/updateMysqlPassword.txt"));
        return random;
    }

    private static void openRemoteConnection(String password) {
        System.out.println("========== 正在开启 Mysql 远程连接 ==========");
        DbUtil.runSql("GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '"+password+"';",password);
        DbUtil.runSql("flush privileges;",password);
        System.out.println("========== 完成开启 Mysql 远程连接 ==========");
        LinuxUtil.openPort(3306);
    }
    public static String installMysql() {
        System.out.println("========== 开始安装 Mysql  ==========");
        System.out.println("========== 正在下载 Mysql rpm ==========");
        RuntimeUtil.getResult(RuntimeUtil.exec("wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm"));
        System.out.println("========== 完成下载 Mysql rpm ==========");
        System.out.println("========== 正在安装 Mysql rpm ==========");
        RuntimeUtil.getResult(RuntimeUtil.exec("yum localinstall mysql57-community-release-el7-11.noarch.rpm -y"));
        System.out.println("========== 安装完毕 Mysql rpm ==========");
        System.out.println("========== 正在安装 Mysql server,时间较长耐心等待... ==========");
        RuntimeUtil.getResult(RuntimeUtil.exec("yum install mysql-community-server -y"));
        System.out.println("========== 安装完成 Mysql server ==========");
        System.out.println("========== 开始启动 Mysql ==========");
        RuntimeUtil.getResult(RuntimeUtil.exec("systemctl start mysqld"));
        System.out.println("========== 启动完成 Mysql ==========");
        System.out.println("========== 设置 Mysql 开机自启 ==========");
        RuntimeUtil.getResult(RuntimeUtil.exec("systemctl enable mysqld"));
        System.out.println("========== 设置 Mysql 开机自启完成 ==========");
        System.out.println("========== 正在关闭 Mysql 密码验证 ==========");
        closeMysqlPassword();
        System.out.println("========== 完成关闭 Mysql 密码验证 ==========");
        System.out.println("========== 正在重启 Mysql server ==========");
        restartMysql();
        System.out.println("========== 完成重启 Mysql server ==========");
        System.out.println("========== 正在设置 Mysql 临时密码 ==========");
        String tempPassword = updateMysqlPassword();
        System.out.println("========== 完成设置 Mysql 临时密码 ==========");
        System.out.println("========== 正在打开 Mysql 密码验证 ==========");
        openMysqlPassword();
        System.out.println("========== 完成打开 Mysql 密码验证 ==========");
        restartMysql();
        System.out.println("========== 正在设置 Mysql 新密码 ==========");
        String password = updateMysqlPassword(tempPassword);
        System.out.println("========== 完成设置 Mysql 新密码 ==========");
        System.out.println("========== 新密码: " + password + " ==========");
        restartMysql();
        openRemoteConnection(password);
        System.out.println("========== 恭喜! Mysql 安装完成 ==========");
        return password;
    }

}
